/*
 * Copyright (c) 2015 DHC Corporation.
 */

package cn.com.dhc.danlu.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

/**
 * 商品详细用ScrollView,为了解决触摸事件冲突问题
 *
 * @author LiYongHe
 * @version 1.00 2015/03/05 新建
 */
public class GoodInfoScrollView extends ScrollView {

    /**
     * 可以滚动
     */
    private boolean canScroll;

    /**
     * 手势识别类
     */
    private GestureDetector mGestureDetector;


    public GoodInfoScrollView (Context context) {
        this (context, null);

    }

    public GoodInfoScrollView (Context context, AttributeSet attrs) {
        this (context, attrs, 0);

    }

    public GoodInfoScrollView (Context context, AttributeSet attrs, int defStyle) {
        super (context, attrs, defStyle);
        mGestureDetector = new GestureDetector (new YScrollDetector ());
        canScroll = true;
        setOverScrollMode (View.OVER_SCROLL_NEVER);
        setHorizontalFadingEdgeEnabled (false);
    }

    @Override
    public boolean onInterceptTouchEvent (MotionEvent ev) {
        if (ev.getAction () == MotionEvent.ACTION_UP) {
            canScroll = true;
        }
        return super.onInterceptTouchEvent (ev)
                && mGestureDetector.onTouchEvent (ev);

    }

    class YScrollDetector extends SimpleOnGestureListener {

        @Override
        public boolean onScroll (MotionEvent e1, MotionEvent e2,
                                 float distanceX, float distanceY) {

            if (canScroll) {
                canScroll = Math.abs (distanceY) >= Math.abs (distanceX);
            }

            return canScroll;
        }

    }

}
